const fs = require('fs');

let editUser = (req, res) => {
    const { gameuserid, username } = req.body;
    let usernameList = require('./datalist/username.json');
    let i =  null;
    usernameList.forEach((item,index) => { if(item.gameuserid == gameuserid) i = index; })
    if(!i) {
      usernameList.push({
        gameuserid,
        formatUserName: username
      })
    }else{
      usernameList[i].formatUserName = username;
    }
    fs.writeFileSync('./datalist/username.json', JSON.stringify(usernameList));
    res.end(JSON.stringify({ code: 1, msg: '用户名修改成功'}));
}

module.exports.editUser = editUser;

